﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Login : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
	protected void Button1_Click(object sender, EventArgs e)
	{
		String userName = this.uname.Value;
		String Password = this.psw.Value;


		string message;

		DataLayer dal = new DataLayer();
        try
        {
            if (!dal.IsAuthenticated(PAConfiguration.ConnectionString, userName, Password, out message))
            {
                if (message != string.Empty)
                {
                    PALogManager.ErrorLog(message);
                    ErrorMessage.Text = message;
                    return;
                }

                ErrorMessage.Text = "Wrong login or password. Please try again.";
                return;
            }
        }
        catch (Exception ex)
        {
            ErrorMessage.Text = ex.Message;
            return;
        }

		Teacher teacher = dal.ReadTeacher(PAConfiguration.ConnectionString, userName, Password, out message);

		if (message != string.Empty)
		{
			PALogManager.ErrorLog(message);
			ErrorMessage.Text = message;
			return;
		}

		SetCurrentTeacher(teacher);

		if (tick.Checked)
		{
			CookiesManager.WriteCookie(Response, userName, Password);
		}

		FormsAuthentication.RedirectFromLoginPage(teacher.TEACHER_ID.ToString(), false);

	}
}